package com.gym.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gym.pojo.Equipment;
import com.gym.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EquipmentController {

    @Autowired
    EquipmentService equipmentService;

    //模糊查询并分页查询
    @RequestMapping("/getEquipmentList")
    @ResponseBody
    public Map<String, Object> getEquipmentList(Integer page, Integer limit, String epStatus) {
        Page<Equipment> equipmentPage = new Page<>(page, limit);
        LambdaQueryWrapper<Equipment> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户名模糊查询
        queryWrapper.like(epStatus != null, Equipment::getEpStatus, epStatus);
        Page<Equipment> equipment = equipmentService.page(equipmentPage, queryWrapper);
        List<Equipment> equipmentList = equipment.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", 0);
        map.put("data", equipmentList);
        //count是total
        map.put("count", equipment.getTotal());
        return map;
    }

    //添加
    @RequestMapping("/addEquipment")
    @ResponseBody
    public Map<String, Object> addEquipment(Equipment equipment) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = equipmentService.save(equipment);
        if (save) {
            map.put("save", save);
            map.put("data", "新增成功");
        } else {
            map.put("save", save);
            map.put("data", "新增失败");
        }
        return map;
    }

    //修改
    @RequestMapping("/editEquipment")
    @ResponseBody
    public Map<String,Object> editEquipment(Equipment equipment) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = equipmentService.updateById(equipment);
        if (save) {
            map.put("save", save);
            map.put("data", "信息更新成功");
        } else {
            map.put("save", save);
            map.put("data", "信息更新失败");
        }
        return map;
    }

    //删除
    @RequestMapping("/deleteEquipment")
    @ResponseBody
    public Map<String,Object> deleteEquipment(String[] epId){
        HashMap<String, Object> map = new HashMap<>();
        //批量删除
        boolean save = equipmentService.removeByIds(Arrays.asList(epId));
        if (save){
            map.put("save",save);
            map.put("data","删除成功");
        }else {
            map.put("save",save);
            map.put("data","删除成功");
        }
        return map;
    }
}
